﻿package com.toncen.samepms.net.domain;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.toncen.samepms.common.AbstractDomain;
import com.toncen.samepms.common.IPropertyUniqueble;
import com.toncen.samepms.common.ITreeable;
import com.toncen.samepms.common.Unique;
import com.toncen.samepms.system.dao.ConfigurationDao;
import com.toncen.samepms.system.domain.Configuration;
import com.sky.common.date.DateTools;
import com.sky.common.string.StringUtils;
import com.sky.dataimport.DynamicCellDataCell;
import com.sky.dataimport.DynamicCellDataRow;
import com.sky.dataimport.DynamicRowDataCell;
import com.sky.dataimport.DynamicRowDataRow;
import com.sky.dataimport.StaticDataCell;
import com.sky.dataimport.StaticDataRow;
import com.sky.dataimport.datatype.DateDataType;
import com.sky.dataimport.datatype.IntegerDataType;
import com.sky.dataimport.datatype.StringDataType;
import com.sky.dataimport.formater.UtilDateDataFormater;

/**
 * 描述：栏目表NET_CHANNEL的domain文件
 * 
 * @time 2013-5-10
 * @author xyp
 */
@DynamicRowDataRow(title = "动态行栏目", sheetIndex = 1, startRowIndex = 2, startCellIndex = 1, inserted = false)
@DynamicCellDataRow(title = "动态列栏目", sheetIndex = 1, startRowIndex = 2, startCellIndex = 2, inserted = false)
@StaticDataRow(title = "静态行列栏目", sheetIndex = 1)
public class Channel extends AbstractDomain implements ITreeable, IPropertyUniqueble {
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1365474907270L;

	/**
	 */
	public static final String PATH_REPLACE = "{path}";

	/**
	 */
	public static final String ID_REPLACE = "{id}";

	/**
	 * 上级栏目
	 */
	@StaticDataCell(rowIndex = 1, cellIndex = 1, title = "上级栏目")
	private Channel parent;

	/**
	 * 名称
	 */
	@StaticDataCell(rowIndex = 2, cellIndex = 2, title = "名称")
	@DynamicRowDataCell(cellIndex = 2, title = "名称", require = false)
	@DynamicCellDataCell(rowIndex = 2, title = "名称", require = false)
	@StringDataType(max = 200)
	@Unique(column = "NAME", title = "名称")
	private String name;

	/**
	 * 链接地址
	 */
	@StaticDataCell(rowIndex = 3, cellIndex = 3, title = "链接地址")
	@DynamicRowDataCell(cellIndex = 3, title = "链接地址", require = false)
	@DynamicCellDataCell(rowIndex = 3, title = "链接地址", require = false)
	@StringDataType(max = 200)
	private String linkUrl="{path}/portal/channel_dsnc.action?domain.id={id}";

	/**
	 * 历史变更时间
	 */
	@StaticDataCell(rowIndex = 4, cellIndex = 4, title = "历史变更时间")
	@DynamicRowDataCell(cellIndex = 4, title = "历史变更时间", formater = UtilDateDataFormater.class, option = "yyyy-MM-dd", require = false)
	@DynamicCellDataCell(rowIndex = 4, title = "历史变更时间", formater = UtilDateDataFormater.class, option = "yyyy-MM-dd", require = false)
	@DateDataType
	private Date historyDate;

	/**
	 * 最后变更时间
	 */
	@StaticDataCell(rowIndex = 5, cellIndex = 5, title = "最后变更时间")
	@DynamicRowDataCell(cellIndex = 5, title = "最后变更时间", formater = UtilDateDataFormater.class, option = "yyyy-MM-dd", require = false)
	@DynamicCellDataCell(rowIndex = 5, title = "最后变更时间", formater = UtilDateDataFormater.class, option = "yyyy-MM-dd", require = false)
	@DateDataType
	private Date lastWriteTime;

	/**
	 * 顺序
	 */
	@StaticDataCell(rowIndex = 6, cellIndex = 6, title = "顺序")
	@DynamicRowDataCell(cellIndex = 6, title = "顺序", require = false)
	@DynamicCellDataCell(rowIndex = 6, title = "顺序", require = false)
	@IntegerDataType(msg = "必须为整数类型")
	private int sequence = 10;
	
	/**
	 * 是否弹出
	 */
	@StaticDataCell(rowIndex = 6, cellIndex = 6, title = "是否弹出")
	@DynamicRowDataCell(cellIndex = 6, title = "是否弹出", require = false)
	@DynamicCellDataCell(rowIndex = 6, title = "是否弹出", require = false)
	@IntegerDataType(msg = "必须为整数类型")
	private int whetherPop = 2;

	/**
	 * 所有栏目（除本身栏目外）
	 */
	private List<Channel> childs = new ArrayList<Channel>();

	private List<RoleChannel> roleChannels = new ArrayList<RoleChannel>();

	public List<RoleChannel> getRoleChannels() {
		return roleChannels;
	}

	public void setRoleChannels(List<RoleChannel> roleChannels) {
		this.roleChannels = roleChannels;
	}

	public Channel() {
		super();
	}

	/**
	 * @return the parent
	 */
	public Channel getParent() {
		return parent;
	}

	/**
	 * @param parent
	 *            the parent to set
	 */
	public void setParent(Channel parent) {
		this.parent = parent;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the linkUrl
	 */
	public String getLinkUrl() {
		return linkUrl;
	}

	/**
	 * @param linkUrl
	 *            the linkUrl to set
	 */
	public void setLinkUrl(String linkUrl) {
		this.linkUrl = linkUrl;
	}

	/**
	 * @return the historyDate
	 */
	public Date getHistoryDate() {
		return historyDate;
	}

	/**
	 * @param historyDate
	 *            the historyDate to set
	 */
	public void setHistoryDate(Date historyDate) {
		this.historyDate = historyDate;
	}

	/**
	 * @return the lastWriteTime
	 */
	public Date getLastWriteTime() {
		return lastWriteTime;
	}

	/**
	 * @param lastWriteTime
	 *            the lastWriteTime to set
	 */
	public void setLastWriteTime(Date lastWriteTime) {
		this.lastWriteTime = lastWriteTime;
	}

	/**
	 * @return the sequence
	 */
	public int getSequence() {
		return sequence;
	}

	/**
	 * @param sequence
	 *            the sequence to set
	 */
	public void setSequence(int sequence) {
		this.sequence = sequence;
	}

	@SuppressWarnings("unchecked")
	public List<ITreeable> getTreeChild() throws Exception {
		return (List) this.getChilds();
	}

	public String getTreeId() throws Exception {
		return String.valueOf(this.getId());
	}

	public String getTreeText() throws Exception {
		return this.getName();
	}

	public List<Channel> getChilds() {
		return childs;
	}

	public void setChilds(List<Channel> childs) {
		this.childs = childs;
	}

	/**
	 * 判断是否最近3天内发布
	 * 
	 * @param publishtime
	 * @return
	 * @throws Exception
	 */
	public boolean isNew() throws Exception {
		boolean isNew = false;
		if (this.getLastWriteTime() != null) {
			Configuration configuration = new ConfigurationDao().getByName("new_information_days");
			int newInformationDays = Integer.parseInt(configuration.getValue());
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			String eDate = sf.format(new Date());
			String sDate = sf.format(this.getLastWriteTime());
			int dayDiff = DateTools.getDayDiff(sDate, eDate);
			isNew = isNew || dayDiff <= newInformationDays;
		}
		for (Channel channel : getChilds()) {
			isNew = isNew || channel.isNew();
		}
		return isNew;
	}

	/**
	 * @return
	 */
	public String getURL() {
		String url=linkUrl;
		url = StringUtils.replaceAll(url, PATH_REPLACE, ServletActionContext.getRequest().getContextPath());
		url = StringUtils.replaceAll(url, ID_REPLACE, String.valueOf(getId()));
		return url;
	}

	public String getPrimaryName() {
		return "ID";
	}

	public String getTableName() {
		return "NET_CHANNEL";
	}

	public int getWhetherPop() {
		return whetherPop;
	}

	public void setWhetherPop(int whetherPop) {
		this.whetherPop = whetherPop;
	}

}
